<?php

//VideoFlow - Joomla Multimedia System for Facebook//
/**
* @ Version 1.1.3 
* @ Copyright (C) 2008 - 2010 Kirungi Fred Fideri at http://www.fidsoft.com
* @ VideoFlow is free software
* @ Visit http://www.fidsoft.com for support
* @ Kirungi Fred Fideri and Fidsoft accept no responsibility arising from use of this software 
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
**/

// No direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport('joomla.application.component.controller');

class VideoflowController extends JController
{
     
      function __construct()
    {
        global $vparams;
        
        parent::__construct();
        $this->registerTask ('report', 'email');   
        $this->registerTask ('eshare', 'email');  
        $this->registerTask ('saveEdit', 'saveRemote');  
        $this->registerTask ('saveFlash', 'saveRemote');
        if ($vparams->facebook) {
        include_once (JPATH_COMPONENT_SITE.DS.'fbook'.DS.'facebook.php');
        $fb = new Facebook($vparams->fbkey, $vparams->fbsecret);
        if($fbuser = $fb->get_loggedin_user()){
        try {
        $fq = 'SELECT status_update,photo_upload,sms,offline_access,email,create_event,rsvp_event,publish_stream,read_stream,share_item,create_note,bookmarked,tab_added FROM permissions WHERE uid = "'.$fbuser.'"';
        JRequest::setVar('perms', $fb->api_client->fql_query($fq));
        } catch (Exception $ex) {
        $fb->clear_cookie_state();
        $fbuser = 0;
        }
        }
        if ($fbuser) JRequest::setVar('fbuser', $fbuser);
      }      
    }

     
     
     

    function display()
    {   
       global $vparams;
       $layout = $this->vlayout();
       $v = JRequest::getCmd ('view');
       if ($v == 'xml') {
       $view = & $this->getView('xml', 'xml');
       $layout = 'default';
       } else {
       $view = & $this->getView('videoflow', 'html');
       }
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> display();
    }
    
            
    function myvids()
    {
        global $vparams;
        $vprocess = $this->vprocess();        
        if ($vprocess->status) {
        $layout = $this->vlayout('_play');  
        $view = & $this->getView('videoflow', 'html');
        $model = & $this->getModel('videoflow');
        $view->setModel($model, true);
        $view->setLayout($layout);
        $view -> displayMyvids($vprocess->myid);
        } else {
        $lo = JRequest::getCmd('layout');
        if ($lo == 'playerview') $lo = 'listview_login'; else $lo = $this->vlayout('_login');  
        if (!empty($lo)) $lo = '&layout='.$lo; else $lo = '';
        $link = html_entity_decode(JRoute::_('index.php?option=com_videoflow&task='.$vprocess->task.'&direct=myvids'.$lo));
        $this->setRedirect($link, $vprocess->message, $vprocess->type);
        }
    }
    
    function myfavs ()
    {
        global $vparams;
        $vprocess = $this->vprocess();        
        if ($vprocess->status) {
        $layout = $this->vlayout('_play');  
        $view = & $this->getView('videoflow', 'html');
        $model = & $this->getModel('videoflow');
        $view->setModel($model, true);
        $view->setLayout($layout);
        $view -> displayMyfavs($vprocess->myid);
        } else {
        $lo = JRequest::getCmd('layout');
        if (!empty($lo)) $lo = '&layout='.$lo; else $lo = '';
        $link = html_entity_decode(JRoute::_('index.php?option=com_videoflow&task='.$vprocess->task.'&direct=myfavs'.$lo));
        $this->setRedirect($link, $vprocess->message, $vprocess->type);
        }
    }

   
    
    function cats()
    {
        global $vparams;    
        $lo = JRequest::getCmd('layout'); 
        $model = & $this->getModel('videoflow');
        $vlist = $model->getData();
        if ($vlist) {
          $view = & $this->getView('videoflow', 'html');
          $view->setModel($model, true);
          if (!empty($vparams->catplay) && $lo != 'playerview') {
          $layout = $this->vlayout('_play');  
          $view->setLayout($layout);
          $view -> displayCats($vlist);
          } else {
          $layout = $this->vlayout();
          $view->setLayout($layout);
          $view->displayByCat($vlist);
          }
        } else {
        $lo = JRequest::getCmd('layout');
        if (!empty($lo)) $lo = '&layout='.$lo; else $lo = '';
        $link = html_entity_decode(JRoute::_('index.php?option=com_videoflow&task=categories'.$lo));
        $err = JText::_('No media found in selected category!');
        $this->setRedirect($link, $err, 'error');
        }    
    }
          
    
    function visit()
    {
        $cid = JRequest::getInt('cid', JRequest::getInt('pid'));
        $layout = $this->vlayout('_play');  
        $view = & $this->getView('videoflow', 'html');
        $model = & $this->getModel('videoflow');
        $fbuser = JRequest::getInt('fbuser');
        if ($fbuser) $vfuser = $model->getVFuserObj($fbuser);
        if (!empty($vfuser)){
        $juser = & JFactory::getUser($vfuser->joomla_id);
        if (!empty($juser->usertype)) JRequest::setVar('juser', $juser);
        } 
        $view->setModel($model, true);
        $view->setLayout($layout);
        $view -> displayMyvids($cid);
    }
    
    function userfavs()
    {
       $layout = $this->vlayout();
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displayUserfavs();
    }
    
    
    function mysubs()
    {
      global $vparams;
        $vprocess = $this->vprocess();        
        $lo = JRequest::getCmd('layout');
        $id = JRequest::getInt('id');
        if ($vprocess->status) {
        if ($lo == 'playerview' && empty($id)) {
        $layout = 'listview_categories';  
        } else {
        $layout = $this->vlayout('_play');  
        }
        $view = & $this->getView('videoflow', 'html');
        $model = & $this->getModel('videoflow');
        $view->setModel($model, true);
        $view->setLayout($layout);
        $view -> displayMysubs($vprocess->myid);
        } else {
        if (!empty($lo)) $lo = '&layout='.$lo; else $lo = '';
        $link = html_entity_decode(JRoute::_('index.php?option=com_videoflow&task='.$vprocess->task.'&direct=mysubs'.$lo));
        $this->setRedirect($link, $vprocess->message, $vprocess->type);
        }
    }


    function login()
    {
      $layout = $this->vlayout ();
      $model = & $this->getModel('videoflow');
      $view = & $this->getView ('videoflow', 'html');
      $view->setModel ($model, true);
      $view->setLayout ($layout);
      $view->displayLogin();
    }
    
    function logout()
    {
      $layout = $this->vlayout ();
      $model = & $this->getModel('videoflow');
      $view = & $this->getView ('videoflow', 'html');
      $view->setModel ($model, true);
      $view->setLayout ($layout);
      $view->displayLogout();
    }

    
    
    function vote()
    {
        $model = & $this->getModel('videoflow' );
        $model->storeVote();
    }
    
    function saveRemote()
    {
      $fbuser = JRequest::getInt('fbuser');
      $model = & $this->getModel('videoflow');
      $status = $model->saveRemote(); 
      if (!empty($status->id) && !empty($fbuser)) {
      $model->createUpfeed($status->id);
      }
      $link = JRoute::_('index.php?option=com_videoflow&task='.$status->task.'&tmpl=component');
      $this->setRedirect($link, $status->message, $status->type);
    }
    
    
    function saveUpload(){

     $model = & $this->getModel('videoflow');
     $model->saveUpload();
    }
    
    function saveXpload(){
     $model = & $this->getModel('videoflow');
     $status = $model->saveXpload();
    }
    
    function saveThumb(){
     $model = & $this->getModel ('videoflow');
     $model->saveThumb();
    }
    

    function getStatus()
    {
     $model = & $this->getModel('videoflow');
     $status = $model->getUploadStatus();
     if ($status->status){
     $link = JRoute::_('index.php?option=com_videoflow&task=edit&cid='.$status->cid.'&userid='.$status->userid.'&tmpl=component&auto=1');
     } else {
     $link = JRoute::_('index.php?option=com_videoflow&task=upload&tmpl=component');
     }
     $this->setRedirect ($link, $status->message, $status->type);
    }
    
    
    function edit()
    {
       global $vparams;
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel ($model, true); 
       $vprocess = $this->vprocess();
       $id = JRequest::getInt('cid');
       $userid = JRequest::getInt('userid');
       $auto = JRequest::getBool('auto');
       $data = $model->getFile($id);
       $mes = JText::_('An error has occurred. Unable to complete request.');
       $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
       switch ($auto){
          case true:
          if (!empty ($data) && $vprocess->myid == $userid && $vprocess->myid == $data->userid) { 
          $view->displayEditForm($data);
          } else {
          $model->setField($id, 'published', '-1');
          $this->setRedirect ($link, $mes, 'error');
          }
          break;
          
          case false:
          if (!empty($data) && $vparams->useredit && $vprocess->myid == $data->userid){
          $view->displayEditForm($data);
          } else if (!empty($data) && ($vprocess->utype == 'Super Administrator' || $vprocess->utype == 'Administrator')) {
          $view->displayEditForm($data);
          } else {
          $this->setRedirect ($link, $mes, 'error');
          }
          break;
       }   
    }
    
            
    function categories()
    {
       $lo = JRequest::getCmd('layout');
       if ($lo == 'playerview') {
       $layout = 'listview_categories';
       } else {
       $layout = $this->vlayout('_categories');
       }
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displayCategories();
    }
    
    function cshare()
    {
       $fb = JRequest::getInt('fbuser', '');
       if ($fb){
       $layout = $this->vlayout('_status');
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displayCshare();
       } else {
       $id = JRequest::getInt('cid');
       $link = JRoute::_('index.php?option=com_videoflow&task=eshare&tmpl=component&id='.$id);
       $this->setRedirect($link);
       }
    }
    
     
    function play()
    {  
       $id = JRequest::getInt('id');
       $layout = $this->vlayout('_play');
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displayMedia($id);
    }    
    
    
    function search()
    {
       $lo = JRequest::getCmd ('layout');
       if ($lo == 'playerview'){
       $layout = $this->vlayout('_search');
       } else {
       $layout = $this->vlayout();
       }
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displaySearch();
    }
    
   function searchplay()
    {  
       $layout = $this->vlayout('_play');
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view->setLayout($layout);
       $view -> displaySearchPlay();
    }    

    
    function email()
    {
       $id = JRequest::getInt('id');
       $view = & $this->getView('videoflow', 'html');
       $model = & $this->getModel('videoflow');
       $view->setModel($model, true);
       $view -> displayEmail($id);
    }
    
    function emailsend()
    {
       $model = & $this->getModel('videoflow');
       $status = $model->emailsend();
       $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
       $this->setRedirect($link, $status->message, $status->type);
    }
    
    function status()
    {
        $layout = $this->vlayout('_status');
        $view = $this->getView('videoflow', 'html');
        $view->setLayout($layout);
        $view->displayStatus();
    }

    function add()
    {
    $status = $this-> vprocess();
    if ($status->status) {
    $model = & $this->getModel('videoflow');
    $status = $model->add($status->myid); 
    }
    $link = JRoute::_('index.php?option=com_videoflow&task='.$status->task.'&tmpl=component');
    if ($status->task == 'login') {
    $id = JRequest::getInt('id');
    $link .= '&layout=listview_login&direct=add&id='.$id;
    }
    $this->setRedirect($link, $status->message, $status->type);
    }
    
    function subscribe()
    {
    $status = $this-> vprocess();
    if ($status->status) {
    $model = & $this->getModel('videoflow');
    $status = $model->subscribe($status->myid); 
    }
    $cid = JRequest::getInt('cid');
    $link = JRoute::_('index.php?option=com_videoflow&task='.$status->task.'&tmpl=component');
    if ($status->task == 'login') $link .= '&layout=listview_login&direct=subscribe&cid='.$cid;
    $this->setRedirect($link, $status->message, $status->type);
    }
    
    function unsubscribe()
    {
      $model = & $this->getModel('videoflow');
      $status = $model->unsubscribe(); 
      $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
      $this->setRedirect($link, $status->message, $status->type);
    }


    
    function upload ()
    {
    $status = $this-> vprocess('sp');
    if ($status->status) {
    $view = & $this->getView('videoflow', 'html');
    $view->displayAdd();
    } else { 
    $link = JRoute::_('index.php?option=com_videoflow&task='.$status->task.'&tmpl=component');
    if ($status->task == 'login') $link .= '&layout=listview_login&direct=upload';
    $this->setRedirect($link, $status->message, $status->type);
    }
    }
    
    function addmedia()
    {
      $status = $this->vprocess('sp');
      $link = JRoute::_('index.php?option=com_videoflow&task='.$status->task.'&tmpl=component');
      if ($status->status){
      $model = & $this->getModel('videoflow');
      $data = $model->addmedia($status->myid);
        if ($data->status){
        $view = & $this->getView('videoflow', 'html');
        $view->setModel($model, true);
        $view->displayEditForm($data);
        } else{
        $status->message = $data->message;
        $status->type = 'error';
        $this->setRedirect($link, $status->message, $status->type);
        }
      } else {
      $this->setRedirect($link, $status->message, $status->type);
      }
    }
    
    function uploadmedia()
    {
      $status = $this->vprocess();
      if ($status->status){
      $model = & $this->getModel('videoflow');
      $data = $model->uploadmedia($status->myid);
        if ($data){
        $view = & $this->getView('videoflow', 'html');
        $view->displayUploadForm($data);
        } else {
        $link = JRoute::_('index.php?option=com_videoflow&task=upload&tmpl=component');
        $status->message = JText::_('An Error occurred. Unable to upload file');
        $this->setRedirect($link, $status->message, $status->type);
        }
      } else {
      $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
      $this->setRedirect($link, $status->message, $status->type);
      }
    }
    
    function remove()
    {
      $model = & $this->getModel('videoflow');
      $status = $model->remove(); 
      $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
      $this->setRedirect($link, $status->message, $status->type);
    }
    
    function delete()
    {
      $model = & $this->getModel('videoflow');
      $status = $model->runTool('runSprocess', 'delete', $model->runTool('createResp'));
      if ($status->status) {     
      $status = $model->delete($status);
      }   
      $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
      $this->setRedirect($link, $status->message, $status->type);
    }

        
    function download()
    {
      $model = & $this->getModel('videoflow');
      $status = $model->getDownload(); 
      if (!$status->status) {
      echo '<script language="javascript" type="text/javascript">alert("'.$status->message.'"); history.go(-1);</script>';
      }
    }  
        
    function vlayout($type = '')
    {
       global $vparams;
       $layout = JRequest::getVar('layout');
       if (($layout == 'playerview' || $layout == 'ajaxlist') && $type == '_play') $type = '';
       if (!empty($layout) && file_exists (JPATH_COMPONENT_SITE.DS.'views'.DS.'videoflow'.DS.'tmpl'.DS.$layout.$type.'.php')) {
       $layout = $layout.$type;
       } elseif (file_exists (JPATH_COMPONENT_SITE.DS.'views'.DS.'videoflow'.DS.'tmpl'.DS.$vparams->jtemplate.$type.'.php')) {
       $layout = $vparams->jtemplate.$type;
       } else{
       $layout = 'listview'.$type;
       }
       return $layout;     
    }
    
    function logincheck()
    {
      $fbuser = JRequest::getInt ('fbuser');
      $juser = & JFactory::getUser();
      if (!$fbuser && $juser->guest){
      $link = JRoute::_('index.php?option=com_videoflow&task=login&tmpl=component&layout=listview_login');
      $message = JText::_('You must login to continue');
      $type = 'error';
      } else {
      $link = JRoute::_('index.php?option=com_videoflow&task=status&tmpl=component');
      $message = JText::_('You are now logged in. Close this window to continue');
      $type = 'message';
      }
      $this->setRedirect($link, $message, $type);
    }
    
    function vprocess($process=null)
    {
    $task = JRequest::getCmd('task');
    $model = & $this->getModel('videoflow' );
    return $model->runVprocess($task, $process);
    }
    
    function fb(){
    echo "<body onLoad=\"alert('".JText::_('You are now logged in with Facebook. Close this window to continue')."'); self.close();\" />";
    }   
}